# -*- coding: utf-8 -*-
"""
Created on Mon Jun 28 21:03:42 2021

@author: Da Hui
"""

import time
import csv
from selenium import webdriver



def run(url,filename):
#    chromedriver的本地地址
    chromedriverPath = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver"
    chromeOptions = webdriver.ChromeOptions()
    prefs = {"profile.managed_default_content_settings.images":2,#禁止加载图片
             'profile.default_content_setting_values':
            {
                'notifications': 2#禁止弹窗
            }}
    chromeOptions.add_experimental_option("prefs",prefs)
    driver = webdriver.Chrome(chromedriverPath,chrome_options=chromeOptions)
    
    driver.get(url)
    time.sleep(10)#等待加载
    #按时间
    time_sort=driver.find_element_by_css_selector('div.WB_feed_repeat.S_bg1.WB_feed_repeat_v3 > div > div.repeat_list > div.tab_feed_a.clearfix.S_line1 > div > ul > li:nth-child(2) > span > a')
    time_sort.click()
    #按时间
    time.sleep(2)
#    采集数据
    comment(driver,filename)
    print("over")
    driver.close()
    
    


def comment(driver,filename): 
    begin=0
    end=45
    window_height=driver.execute_script("return document.body.scrollHeight;") #当前页面的最大高度
    check_height=0
#    加载所有评论
    while check_height != window_height:
        check_height=window_height
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")#滑动到底部
        time.sleep(3)#加载新的评论
        window_height=driver.execute_script("return document.body.scrollHeight;")
            
#    查看更多
    modo='div.WB_feed_repeat.S_bg1.WB_feed_repeat_v3 > div > div.repeat_list > div:nth-child(2) > div > div > a'
    btn=driver.find_elements_by_css_selector(modo)
    ul_box='div.WB_feed_repeat.S_bg1.WB_feed_repeat_v3 > div > div.repeat_list > div:nth-child(2) > div > div > div'
    
    text_select='div.list_con > div.WB_text'
    time_select='div.WB_func.clearfix > div.WB_from.S_txt2'
    
#    当可以“查看更多”时
    while(len(btn) != 0):
        try:
            val=[]
            ul=driver.find_elements_by_css_selector(ul_box)
#            第一屏最多展示45条
            for i in range(begin,end):
                Text=ul[i].find_element_by_css_selector(text_select)
                Time=ul[i].find_element_by_css_selector(time_select)
                content=Text.text.split('：')[1]
                timee=Time.text
                val.append([timee,content])
        except IndexError:
            pass 
        
#        print(len(val))
#            打印数据
        print2CSV(val,filename)
        
        begin=len(ul)+1
        end=begin+15#点击一次“查看更多”显示15条
#        print(btn[0])
        btn[0].click()
        time.sleep(2)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(2)
        btn=driver.find_elements_by_css_selector(modo)

   
def print2CSV(val,filename):
#    print("outputing...")
    file = open(filename+'.csv', 'a+', newline='', encoding='utf-8-sig')
    writer = csv.writer(file)
    for item in val:
        writer.writerow([item[0], item[1]])


if __name__ == '__main__':
    title=[
#            '五星图像',
#           '再现觉醒年代',
#           '开场烟花',
#           '致敬改革开放',
#           '重温港澳回归',
#           '向我开炮',
#           '完整版觉醒年代',
#           '绝美烟花',
#           '强军战歌',
#           '脱贫',
#           '央视直播',
#           '人民日报节目单',
#           '人民日报直播',
#           '央视节目单',
#           '鸟巢文艺演出焰火超清收藏版',
#           '鸟巢演出版唱支山歌给党听',
#           '鸟巢万人高唱没有共产党就没有新中国',
#           '1921至2021绽放在鸟巢上空',
#           '鸟巢演出合唱新的天地',
#           '100面大鼓在鸟巢上空飞舞',
#           '这不是复制粘贴这是强军战歌',
#           '鸟巢大屏幕出现钟南山等抗疫英雄',
#           '鸟巢还原援鄂军机舱门打开瞬间',
#           '献给全面小康的赞歌',
#           '鸟巢演出合唱江山_jingxuan',
#           '鸟巢再次唱起我和你',
#           '鸟巢重现飞天点火',
#           '鸟巢演出合唱走进新时代',
#           '鸟巢演出合唱春天的故事_jingxxuan',
#           '鸟巢重现香港澳门回归时刻',
#           '青年代表合唱与我同行',
#           '情景再现为了胜利向我开炮',
#           '开国大典烟花镜头',
#           '毛爷爷的声音加彩色的开国大典',
#           '五星烟花配合开国大典绽放',
#           '鸟巢齐唱保卫黄河太震撼了',
#           '鸟巢重现觉醒年代情节',
#           '国家体育场响起国际歌',
#           '鸟巢里首次架起180米超大屏幕',
#           '舞台再现李大钊陈独秀对话',
           '鸟巢烟花打出100字样'
           ]
    url=[
#         'https://weibo.com/2656274875/KmLjrqOXO?filter=hot&root_comment_id=0&type=comment', #开国大典时出现五星图像
#         'https://weibo.com/2656274875/KmL9cshGg?filter=hot&root_comment_id=0&type=comment#_rnd1625146280966', #再现觉醒年代
#         'https://weibo.com/2656274875/KmL897ZOe?filter=hot&root_comment_id=0&type=comment',#开场烟花
#         'https://weibo.com/2656274875/KmLBtfv4s?filter=hot&root_comment_id=0&type=comment',#致敬改革开放
#         'https://weibo.com/2656274875/KmLyCgB0W?filter=hot&root_comment_id=0&type=comment',#重温港澳回归
#         'https://weibo.com/2656274875/KmLntq10b?filter=hot&root_comment_id=0&type=comment',#向我开炮
#         'https://weibo.com/2656274875/KmLcDrY8n?filter=hot&root_comment_id=0&type=comment',#完整版觉醒年代
#         'https://weibo.com/2656274875/KmLW3yHFZ?filter=hot&root_comment_id=0&type=comment',#绝美烟花
#         'https://weibo.com/2656274875/KmLNAnSJC?filter=hot&root_comment_id=0&type=comment',#强军战歌
#         'https://weibo.com/2656274875/KmLEX0nai?filter=hot&root_comment_id=0&type=comment',#脱贫
#         'https://weibo.com/2656274875/KmL4V7kHp?filter=hot&root_comment_id=0&type=comment', #央视 直播
#         'https://weibo.com/2803301701/KmKGEbWtD?type=comment#_rnd1625141079270',#人民日报 节目单
#         'https://weibo.com/2803301701/KmL4Wh4fu?filter=hot&root_comment_id=0&type=comment', #人民日报 直播
#         'https://weibo.com/2656274875/KmKCwfW1f?type=comment#_rnd1625140592636', #央视节目单
#         'https://weibo.com/2803301701/KmMt8ziHN?filter=hot&root_comment_id=0&type=comment',#鸟巢文艺演出焰火超清收藏版
#         'https://weibo.com/2803301701/KmM3hf46c?filter=hot&root_comment_id=0&type=comment#_rnd1625180732487', #鸟巢演出版唱支山歌给党听
#         'https://weibo.com/2803301701/KmLVydiNv?filter=hot&root_comment_id=0&type=comment#_rnd1625180767805', #鸟巢万人高唱没有共产党就没有新中国
#         'https://weibo.com/2803301701/KmLV6rILx?filter=hot&root_comment_id=0&type=comment#_rnd1625180797428', #1921至2021绽放在鸟巢上空
#         'https://weibo.com/2803301701/KmLRKFyIW?filter=hot&root_comment_id=0&type=comment',#鸟巢演出合唱新的天地_jinxuan
#         'https://weibo.com/2803301701/KmLOdaXiX?filter=hot&root_comment_id=0&type=comment',#100面大鼓在鸟巢上空飞舞
#         'https://weibo.com/2803301701/KmLLpmAq4?filter=hot&root_comment_id=0&type=comment', #这不是复制粘贴这是强军战歌
#         'https://weibo.com/2803301701/KmLK202MH?filter=hot&root_comment_id=0&type=comment', #鸟巢大屏幕出现钟南山等抗疫英雄
#         'https://weibo.com/2803301701/KmLHgCQ1f?filter=hot&root_comment_id=0&type=comment', #鸟巢还原援鄂军机舱门打开瞬间
#         'https://weibo.com/2803301701/KmLGgvgNE?filter=hot&root_comment_id=0&type=comment', #献给全面小康的赞歌
#         'https://weibo.com/2803301701/KmLDD889F?filter=hot&root_comment_id=0&type=comment#_rnd1625180966528' #鸟巢演出合唱江山_jingxuan
#         'https://weibo.com/2803301701/KmLBThSbT?filter=hot&root_comment_id=0&type=comment', #鸟巢再次唱起我和你
#         'https://weibo.com/2803301701/KmLAzu2ht?filter=hot&root_comment_id=0&type=comment#_rnd1625180580324', #鸟巢重现飞天点火
#         'https://weibo.com/2803301701/KmLzEq34n?filter=hot&root_comment_id=0&type=comment', #鸟巢演出合唱走进新时代
#         'https://weibo.com/2803301701/KmLvmcXEe?filter=hot&root_comment_id=0&type=comment', #鸟巢演出合唱春天的故事_jingxxuan
#         'https://weibo.com/2803301701/KmLvcbgDE?filter=hot&root_comment_id=0&type=comment', #鸟巢重现香港澳门回归时刻
#         'https://weibo.com/2803301701/KmLtmkUIf?filter=hot&root_comment_id=0&type=comment', #青年代表合唱与我同行
#         'https://weibo.com/2803301701/KmLoCk1FM?filter=hot&root_comment_id=0&type=comment', #情景再现为了胜利向我开炮
#         'https://weibo.com/2803301701/KmLmUwMwI?filter=hot&root_comment_id=0&type=comment', #开国大典烟花镜头
#         'https://weibo.com/2803301701/KmLlOjzRO?filter=hot&root_comment_id=0&type=comment', #毛爷爷的声音加彩色的开国大典
#         'https://weibo.com/2803301701/KmLiFANey?filter=hot&root_comment_id=0&type=comment#_rnd1625180639317', #五星烟花配合开国大典绽放
#         'https://weibo.com/2803301701/KmLheibha?filter=hot&root_comment_id=0&type=comment', #鸟巢齐唱保卫黄河太震撼了
#         'https://weibo.com/2803301701/KmLdRnnMm?filter=hot&root_comment_id=0&type=comment#_rnd1625180650279', #鸟巢重现觉醒年代情节
#         'https://weibo.com/2803301701/KmLcMAolN?filter=hot&root_comment_id=0&type=comment', #国家体育场响起国际歌
#         'https://weibo.com/2803301701/KmLcj6ZZp?filter=hot&root_comment_id=0&type=comment#_rnd1625180659317', #鸟巢里首次架起180米超大屏幕舞台
#         'https://weibo.com/2803301701/KmL9Diq1A?filter=hot&root_comment_id=0&type=comment#_rnd1625180664357', #舞台再现李大钊陈独秀对话
         'https://weibo.com/2803301701/KmL81rZ9m?filter=hot&root_comment_id=0&type=comment', #鸟巢烟花打出100字样
         ] 
    
    for i in range(len(url)):
#        创建以话题为名的文件
        file = open(title[i]+'.csv', 'w+', newline='', encoding='utf-8-sig')
#        写入表头
        writer = csv.writer(file)
        writer.writerow(['time','content'])
        run(url[i],title[i])
   